package com.webserver.http;

import static com.webserver.http.HttpContext.CR;
import static com.webserver.http.HttpContext.LF;
import static com.webserver.http.HttpContext.getMimeType;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示HTTP协议规定的一个响应内容.
 * 每个响应都由三部分构成:
 * 状态行,响应头,响应正文
 */

public class HttpServletResponse {
    //状态行
    private int statusCode = 200;
    private String statusReason = "ok";
    //响应头
    private Map<String,String> headers = new HashMap<>();

    //响应正文
    private File contentFile;

    private Socket socket;

    public HttpServletResponse(Socket socket) {
        this.socket = socket;
    }

    public void response() throws IOException {
        //3.1发送状态行
        sendStatusLine();
        //3.2发送响应头
        sendHeaders();
        //3.3发送响应正文
        sendContent();
        System.out.println("响应发送完毕!");
    }

    //发送状态行
    private void sendStatusLine() throws IOException {
        String line = "HTTP/1.1" + " " + statusCode + " " + statusReason;
        println(line);
    }

    //发送响应头
    private void sendHeaders() throws IOException {
//        println("Content-Type: text/html");
//        println("Con8464896149" +
//                "43tent-Length: " + contentFile.length());
//        println("");

        Set<Map.Entry<String,String>> headerSet = headers.entrySet();
        for (Map.Entry<String,String> e : headerSet){
            String key = e.getKey();
            String value = e.getValue();
            println(key+": "+value);
        }
        println("");
    }

    //发送响应正文
    private void sendContent() throws IOException {
        OutputStream out = socket.getOutputStream();
        try (FileInputStream fis = new FileInputStream(contentFile);) {
            byte[] data = new byte[1024 * 10];
            int len;
            while ((len = fis.read(data)) != -1) {
                out.write(data, 0, len);
            }
        }

    }

    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        //
        out.write(data);
        out.write(CR);//单独发送回车符
        out.write(LF);//单独发送换行符
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
        String fileName = contentFile.getName();
        String[] da = fileName.split("\\.");
        //String ext = fileName.substring(fileName.lastIndexOf("."))

        addHeader("Content-Type", getMimeType(da[da.length - 1]));
        addHeader("Content-Length", contentFile.length() + "");
    }

    public void addHeader(String name,String value){
        this.headers.put(name,value);
    }
}